package hhs.poc.activities.whoiswho;

import hhs.poc.activities.R;
import hhs.poc.util.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;

public class Menu extends Activity {
	private Button schedule_button;
	private Button whoiswho_button;
	private Button reservation_button;
	private String room_nr;
	private ArrayList<Employee> employees = new ArrayList<Employee>();
	private Spinner spinner = null;
	private Intent i;
	private DatabaseManager db;
	private SpinAdapter adapter;
	private MyOnItemSelectedListener item_listener;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new DatabaseManager(this);

		Bundle extras = getIntent().getExtras();
		room_nr = extras.getString("room_nr");
		Log.println(Log.ASSERT, "room_nr", String.valueOf(room_nr));
		employees = db.getEmployeesByRoom(room_nr);

		item_listener = new MyOnItemSelectedListener();

		initComponents();

		schedule_button = (Button) findViewById(R.id.schedule_button);
		setButtonListener(schedule_button, Menu.this, List.class, "schedule");

		whoiswho_button = (Button) findViewById(R.id.whoiswho_button);
		setButtonListener(whoiswho_button, Menu.this, Whoiswho.class,
				"whoiswho");

		reservation_button = (Button) findViewById(R.id.reservation_button);
		setButtonListener(reservation_button, Menu.this, List.class,
				"reservation");

		if (!employees.isEmpty()) {
			spinner = (Spinner) findViewById(R.id.spinner);

			ArrayList<String> names = new ArrayList<String>();
			for (Employee emp : employees) {
				names.add(emp.getName());
			}

			adapter = new SpinAdapter(this,
					android.R.layout.simple_spinner_item, employees, names);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);

			schedule_button.setEnabled(true);
			whoiswho_button.setEnabled(true);
		}

		String date = new SimpleDateFormat("dd-MM-yyyy", new Locale("nl"))
				.format(new Date());
		ArrayList<Appointment> appointments = db.getAppointmentsByRoom(room_nr,
				date);
		if (!appointments.isEmpty()) {
			reservation_button.setEnabled(true);
		}
	}

	private void initComponents() {
		utils.setTitleBackgroundColor(findViewById(android.R.id.title));
		setContentView(R.layout.menu);
	}

	public void setButtonListener(Button button, final Activity current,
			final Class<?> target, final String act_type) {
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				i = new Intent(current, target);
				i.putExtra("room_nr", room_nr);
				if (act_type.equals("reservation")) {
					i.putExtra("act_type", "reservation");
					startActivity(i);
				} else {
					if (spinner != null) {
						spinner.setOnItemSelectedListener(item_listener);
						if (act_type.equals("schedule")) {
							i.putExtra("act_type", "schedule");
						}
						spinner.performClick();
						adapter.setIntent(i);
					}
				}
			}
		});
	}

	private class MyOnItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			Employee emp = adapter.getEmployee(position);
			utils.saveEmployee(emp);
			startActivity(i);
		}

		public void onNothingSelected(AdapterView parent) {
			Log.println(Log.ASSERT, "onNothingSelected", parent.toString());
			// Do nothing.
		}
	}
}